package com.hzit.hzb.core.controller.api;


import com.hzit.common.result.R;
import com.hzit.hzb.base.util.JwtUtils;
import com.hzit.hzb.core.pojo.vo.BorrowerVO;
import com.hzit.hzb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/borrower")
@Api(tags = "借款人接口")
@Slf4j
public class BorrowerController {

	@Autowired
	private BorrowerService borrowerService;

	/**
	 * 保存借款人信息
	 * @param vo
	 * @return
	 */
	@PostMapping("save")
	public R save(@RequestBody BorrowerVO vo, @RequestHeader("token") String token){
		Long userId = JwtUtils.getUserId(token);
		borrowerService.saveBorrowerVOByUserId(vo,userId);
		return R.ok().message("借款人信息添加成功！");
	}

	/**
	 * 获取借款人的状态信息
	 */
	@GetMapping("/auth/getBorrowerStatus")
	public R getBorrowerStatus(@RequestHeader("token") String token){
		// 1. 获取用户id
		Long userId = JwtUtils.getUserId(token);
		// 2. 根据用户id获取借款人状态值
		Integer status = borrowerService.getBorrowerStatus(userId);
		// 3. 返回状态值
		return R.ok().data("borrowerStatus",status);
	}

	/**
	 * 获取借款额度
	 */
	@GetMapping("/getBorrowAmount")
	public R getBorrowAmount(@RequestHeader("token") String token){
		// 1. 获取token值,根据token值获聂userId
		Long userId = JwtUtils.getUserId(token);
		// 2. 根据userId获取借款额度
		BigDecimal borrowAmount = borrowerService.getBorrowAmount(userId);
		// 3. 返回
		return R.ok().data("borrowAmount",borrowAmount);
	}
}

